package com.demo.passenger.hotel.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.passenger.hotel.entity.HotelInfo;
import com.demo.passenger.hotel.service.IHotelInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 旅店信息表 前端控制器
 * </p>
 *
 * @author chenlw
 * @since 2019-07-20
 */
@RestController
@RequestMapping("/api/hotel")
public class HotelInfoRestController {
    @Autowired
    IHotelInfoService hotelInfoService;


    @RequestMapping("/listByParams")
    public Page<HotelInfo> listByParams(HotelInfo hotelInfo,
                                        @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                        @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return hotelInfoService.listByParams(hotelInfo, current, size);
    }

    @CrossOrigin(allowCredentials = "true")
    @GetMapping("/listAll")
    public List<HotelInfo> listAll() {
        QueryWrapper<HotelInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("CREATE_TIME");
        return hotelInfoService.list(queryWrapper);
    }

    @CrossOrigin(allowCredentials = "true")
    @RequestMapping("/listAllSortByCreateTime")
    public List<HotelInfo> listAllSortByCreateTime() {
        return hotelInfoService.listAllSortByCreateTime();
    }

    @RequestMapping("/save")
    public Boolean save(HotelInfo hotelInfo) {
        hotelInfo.setUpdateTime(new Date());
        return hotelInfoService.saveOrUpdate(hotelInfo);
    }

    @RequestMapping("/update")
    public Boolean update(HotelInfo hotelInfo) {
        hotelInfo.setUpdateTime(new Date());
        return hotelInfoService.saveOrUpdate(hotelInfo);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Boolean delete(HotelInfo hotelInfo) {
        return hotelInfoService.removeById(hotelInfo.getId());
    }

    @RequestMapping("/read")
    public HotelInfo read(HotelInfo hotelInfo) {
        if (StrUtil.isNotEmpty(hotelInfo.getId())) {
            hotelInfo = hotelInfoService.getById(hotelInfo.getId());
        }
        return hotelInfo;
    }
}
